package nlp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import org.tartarus.snowball.ext.porterStemmer;

import weka.core.tokenizers.NGramTokenizer;

public class MoodwordsFeatureCreator implements FeatureCreator {

	private String textString;
	private porterStemmer stemmer;
	private HashSet<String> moodWordSet;

	public MoodwordsFeatureCreator(String textString, porterStemmer stemmer,
			HashSet<String> moodWordSet) {
		this.textString = textString;
		this.stemmer = stemmer;
		this.moodWordSet = moodWordSet;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void CreateFeature(Object obj) {
		NGramTokenizer ngramTokenizer = new NGramTokenizer();
		ngramTokenizer.tokenize(textString);
		ngramTokenizer.setNGramMaxSize(1);
		ngramTokenizer.setNGramMinSize(1);

		List<String> moodWords = new ArrayList<String>();
		int numTokens = 0;
		while (ngramTokenizer.hasMoreElements()) {
			String token = (String) ngramTokenizer.nextElement();
			this.stemmer.setCurrent(token.toLowerCase());
			this.stemmer.stem();
			String stemmedToken = stemmer.getCurrent();
			if (this.moodWordSet.contains(stemmedToken)) {
				moodWords.add(stemmedToken);
			}
			++numTokens;
		}

		FreqDistUtil freqDistUtil = new FreqDistUtil(moodWords, numTokens);
		List<FreqData> freqDist = freqDistUtil.getAll();
		HashMap<String, Feature> featureMap = (HashMap<String, Feature>) obj;
		for (FreqData data : freqDist) {
			Feature feature = new MoodwordsFeature(data.getData(), data
					.getFrequency());
			featureMap.put(feature.GetFeatureName(), feature);
		}
	}

	@Override
	public String GetFeatureName() {
		return "MoodWord";
	}

}
